package com.example.demo.utils;

import java.security.KeyPair;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.TimeUnit;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.example.demo.exception.BusinessException;
import com.example.demo.constants.BusinessExceptionCode;
import com.example.demo.tool.FastJsonUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.ListOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SetOperations;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Repository;
@Repository
public class RedisUtil {

    @Autowired
    private StringRedisTemplate template;

    @Autowired
    private  RedisTemplate redisTemplate;

    /**
     *	 存放键值对
     * @param key
     * @param value
     */
    public  void setKey(String key,String value){
        ValueOperations<String, String> ops = template.opsForValue();
        ops.set(key,value);//1分钟过期
    }

    public void setKeyOfList(String key, List<String> value){
        ListOperations listOperations = redisTemplate.opsForList();
        listOperations.leftPushAll(key,value);
    }

    public List<String> getKeyOfList(String key){
        ListOperations listOperations = redisTemplate.opsForList();
        return listOperations.range(key,0,-1);
    }
    /**
     * 	 存放键值对
     * @param key
     * @param value
     * @param ms 过期时间
     */
    public  void setKeyAndExpire(String key,String value,long ms){
        ValueOperations<String, String> ops = template.opsForValue();
        ops.set(key,value,ms,TimeUnit.MILLISECONDS);
    }

    /**
     * 	 存放键值对
     * @param key
     * @param value
     * @param ms 过期时间
     */
    public  void setKeyAndExpireMILLIDays(String key,String value,long ms){
        ValueOperations<String, String> ops = template.opsForValue();
        ops.set(key,value,ms,TimeUnit.DAYS);
    }

    public void saveRSAKeyPair(String key,KeyPair keyPair,long ms) {
        if (!ObjectUtils.isEmpty(keyPair)) {
            redisTemplate.opsForValue().setIfAbsent(key, keyPair, ms,TimeUnit.DAYS);
        }
    }


    public KeyPair getRSAKeyPair(String key) {
        KeyPair keyPair = (KeyPair) redisTemplate.opsForValue().get(key);
        return (KeyPair) redisTemplate.opsForValue().get(key);
    }

    /**
     * 	根据key拿值
     * @param key
     * @return
     */

    public String getValue(String key){
        ValueOperations<String, String> ops = template.opsForValue();
        return ops.get(key);
    }

    /**
     * 从map中获取对应key的value
     * @param realKey
     * @param mapKey
     * @return
     */
    public String getValueForMap(String realKey, String mapKey){
        try {
            String s = template.opsForValue().get(realKey);
            Map<String, String> cacheMap = FastJsonUtils.toBean(s, Map.class);
            if (cacheMap.containsKey(mapKey)){
                return cacheMap.get(mapKey);
            }
            return "";
        }catch (Exception e){
            throw new BusinessException(BusinessExceptionCode.REDIS_WRITE_ERROR_CODE);
        }
    }
    /**
     * 获取map
     * @param realKey
     * @return
     */
    public Map getMap(String realKey){
        try {
            String s = template.opsForValue().get(realKey);
            Map cacheMap = FastJsonUtils.toBean(s, Map.class);
            return cacheMap;
        }catch (Exception e){
            throw new BusinessException(BusinessExceptionCode.REDIS_WRITE_ERROR_CODE);
        }
    }

    /**
     * 将map写入redis
     * @param realKey
     * @param map
     */
    public void setMap(String realKey, Map map){
        try {
            template.delete(realKey);
            template.opsForValue().set(realKey, FastJsonUtils.collectToString(map));
        }catch (Exception e){
            throw new BusinessException(BusinessExceptionCode.REDIS_READ_ERROR_CODE);
        }
    }

    public Boolean existKey(String key){
        return this.template.hasKey(key);
    }

    public Long insertList(String key,List<Integer> list){
    	 return redisTemplate.opsForList().leftPushAll(key, list);
    }

    public Long insertListOne(String key,int id){
   	 return redisTemplate.opsForList().leftPush(key, id);
   }

    public Long insertListStringOne(String key,String id){
      	 return redisTemplate.opsForList().leftPush(key, id);
      }

    public List<Integer> findList(String key){
    	return redisTemplate.opsForList().range(key,0,-1);
   }

    public List<String> findListString(String key){
    	return redisTemplate.opsForList().range(key,0,-1);
   }

    public Boolean delKey(String key){
    	return template.delete(key);
    }

    public void delKeyList(String key,int value){
    	redisTemplate.opsForList().remove(key, 1, value);
    }
    public void delKeyListString(String key,String value){
    	redisTemplate.opsForList().remove(key, 1, value);
    }
    public void setexpire(String key,Long expire){
    	template.expire(key,expire,TimeUnit.MILLISECONDS);
    }


}